package com.sfx.admin.controller;

import com.sfx.admin.common.convention.result.Result;
import com.sfx.admin.common.convention.result.Results;
import com.sfx.admin.dto.req.UserLoginReqDTO;
import com.sfx.admin.dto.req.UserRegisterReqDTO;
import com.sfx.admin.dto.req.UserUpdateReqDTO;
import com.sfx.admin.dto.resp.ActualUserRespDTO;
import com.sfx.admin.dto.resp.UserLoginRespDTO;
import com.sfx.admin.dto.resp.UserRespDTO;
import com.sfx.admin.service.IUserService;
import com.sfx.admin.common.utils.BeanUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
public class UserController {

    private final IUserService userService;

    @GetMapping("/api/short-link/admin/v1/user/{username}")
    public Result<UserRespDTO> getUserByUserName(@PathVariable("username") String username) {
        return Results.success(userService.getUserByUserName(username));
    }

    @GetMapping("/api/short-link/admin/v1/actual/user/{username}")
    public Result<ActualUserRespDTO> getActualUserByUserName(@PathVariable("username") String username) {
        return Results.success(BeanUtils.convert(userService.getUserByUserName(username), ActualUserRespDTO.class));
    }

    @GetMapping("/api/short-link/admin/v1/user/has-username")
    public Result<Boolean> hasUsername(@RequestParam("username") String username) {
        return Results.success(userService.hasUsername(username));
    }

    @PostMapping("/api/short-link/admin/v1/user")
    public Result<Void> register(@RequestBody UserRegisterReqDTO requestParam) {
        userService.register(requestParam);
        return Results.success();
    }

    @PutMapping("/api/short-link/admin/v1/user")
    public Result<Void> update(@RequestBody UserUpdateReqDTO requestParam) {
        userService.update(requestParam);
        return Results.success();
    }

    @PostMapping("/api/short-link/admin/v1/user/login")
    public Result<UserLoginRespDTO> login(@RequestBody UserLoginReqDTO requestParam) {
        return Results.success(userService.login(requestParam));
    }

    @GetMapping("/api/short-link/admin/v1/user/check-login")
    public Result<Boolean> checkLogin(String username, String token) {
        return Results.success(userService.checkLogin(username, token));
    }

    @DeleteMapping("/api/short-link/admin/v1/user/logout")
    public Result<Void> logout(String username, String token) {
        userService.logout(username, token);
        return Results.success();
    }
}